package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.OvertimeApprovalDao;
import com.hhs.storage.dao.WorkOvertimeDao;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.OvertimeApprovalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.service.WorkOvertimeService;
import com.hhs.storage.service.WorkOvertimeTimeService;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chx
 * @since 2024-01-26
 */
@Service
public class OvertimeApprovalServiceImpl extends ServiceImpl<OvertimeApprovalDao, OvertimeApproval> implements OvertimeApprovalService {

    @Resource
    private HttpServletRequest request;

    @Resource
    public WorkOvertimeService workOvertimeService;

    @Resource
    public WorkOvertimeTimeService workOvertimeTimeService;

    @Resource
    private WorkOvertimeDao workOvertimeDao;

    @Resource
    private BaseUserService baseUserService;

    @Resource
    private OvertimeApprovalDao overtimeApprovalDao;

    @Override
    public Map<String, Object> getAll(WorkOvertime workOvertime, InputParam inputParam) {
        LambdaQueryWrapper<WorkOvertime> qw = new LambdaQueryWrapper();

        if (!StringUtil.iszNull(workOvertime.getName())) {
            workOvertime.setName("%" + workOvertime.getName() + "%");
        }

        if (inputParam.getPage() != null) {
            inputParam.setPage((inputParam.getPage() - 1) + (inputParam.getSize() * (inputParam.getPage() - 1)));
        }

        List<WorkOvertime> all = overtimeApprovalDao.getAll(workOvertime.getName(), workOvertime.getApprovalStatus(), inputParam.getPage(), inputParam.getSize());
        long total = overtimeApprovalDao.getAllTotal(workOvertime.getName(), workOvertime.getApprovalStatus());

        qw.eq(WorkOvertime::getApprovalStatus, workOvertime.getApprovalStatus());

        all.forEach(obj -> {
            BaseUser baObj = baseUserService.getById(obj.getUserId());
            obj.setName(baObj.getName());
            obj.setAccount(baObj.getAccount());
            obj.setDepartment(workOvertimeDao.getMyDepartment(obj.getUserId()));
            LambdaQueryWrapper<WorkOvertimeTime> qw3 = new LambdaQueryWrapper<>();
            qw3.eq(WorkOvertimeTime::getWorkId, obj.getId());
            List<WorkOvertimeTime> list1 = workOvertimeTimeService.list(qw3);
            obj.setTimeQuantum(list1);
        });

        Map<String, Object> map = new HashMap<>();
        map.put("code", 1);
        map.put("rows", all);
        map.put("total", total);
        return map;
    }

    @Override
    public Map<String, Object> approval(WorkOvertime workOvertime) {
        String loginUserId = SysUtil.getLoginUserId(request);
        LambdaUpdateWrapper<WorkOvertime> luw = new LambdaUpdateWrapper<>();
        luw.eq(WorkOvertime::getUserId, workOvertime.getUserId());
        luw.set(WorkOvertime::getId, SysUtil.UUID());
        luw.set(WorkOvertime::getApprovalDateTime, LocalDateTime.now());
        luw.set(WorkOvertime::getUpdateTime, LocalDateTime.now());
        luw.set(WorkOvertime::getApprovalId, loginUserId);
        luw.set(WorkOvertime::getApprovalStatus, workOvertime.getApprovalStatus());
        luw.set(WorkOvertime::getApprovalOpinion, workOvertime.getApprovalOpinion());
        boolean update = workOvertimeService.update(luw);

        if (!update) {
            return ResponseInfo.error("修改失败");
        }

        return ResponseInfo.success("修改成功");
    }

}
